<template>
  <div id="flight-orders-to-print">
    <div class="card">
      <div class="card-header bg-info text-white">待打印机票订单</div>
      <div class="card-body">
        <form>
          <div class="row">
            <div class="col">
              <my-date-picker
                id="beginDate"
                v-model="beginDate"
                name="sc.beginDate"
                placeholder="开始出票日期"
              ></my-date-picker>
            </div>
            <div class="col">
              <my-date-picker
                id="endDate"
                v-model="endDate"
                name="sc.endDate"
                placeholder="截止出票日期"
              ></my-date-picker>
            </div>
            <div class="col">
              <select class="form-select ms-1" v-model.number="itineraryType">
                <option value="-1">全部类型</option>
                <option value="1">行程单</option>
                <option value="2">发票</option>
                <option value="3">信息单</option>
              </select>
            </div>
            <div class="col">
              <select class="form-select ms-1" v-model.number="printCount">
                <option value="-1">所有打印状态</option>
                <option value="0">未打印</option>
              </select>
            </div>
            <div class="col">
              <select class="form-select ms-1" v-model.number="intlTicket">
                <option value="-1">所有</option>
                <option value="0">国内</option>
                <option value="1">国际</option>
              </select>
            </div>
            <div class="col">
              <my-select-customer
                :customerId.sync="customerId"
                :status="1"
              ></my-select-customer>
            </div>
            <div class="col">
              <button
                type="button"
                class="btn btn-primary ms-2"
                @click.stop="search()"
              >
                查找
              </button>
              <button
                type="button"
                class="btn btn-secondary btn-sm ms-2"
                @click.stop="reset()"
              >
                重置
              </button>
            </div>
          </div>
        </form>
      </div>
    </div>

    <table
      id="flight-order-list"
      class="table table-sm table-striped table-hover"
    >
      <thead>
        <tr>
          <th>出票日期</th>
          <th>公司</th>
          <th>国际?</th>
          <th>乘机人</th>
          <th>编码</th>
          <th colspan="6" class="bg-info text-center text-white">行程</th>
          <th class="text-end">销售价</th>
          <th class="text-end">税</th>
          <th class="text-end">服务费</th>
          <th class="text-end">订单金额</th>
          <th>付款方式</th>
          <th>行程单?</th>
          <th>状态</th>
          <th>打印次数</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <template v-for="(info, index) in dataList">
          <tr :class="{ 'text-muted': info.status === 4 }">
            <td>{{ info.etdzDate }}</td>
            <td>
              <template v-if="info.customerType === 0"> 散客 </template>
              <template v-else>
                {{ info.customer.vipName }}
                <span class="text-danger small">{{
                  info.customer.customerCode
                }}</span>
              </template>
            </td>
            <td>
              <span class="text-danger" v-if="info.intlTicket === 1">国际</span>
              <span v-else>国内</span>
            </td>
            <td>
              <template v-for="(psg, index) in info.passengers">
                {{ psg.name }} {{ psg.ticketNo }}
                <template v-if="index < info.passengers.length - 1"
                  ><br
                /></template>
              </template>
            </td>
            <td>{{ info.pnrNo }}</td>
            <td>
              <template v-for="(flt, index) in info.flights">
                <small class="text-success">{{ flt.dport }}</small>
                {{ flt.dportName }}
                <template v-if="index < info.flights.length - 1"
                  ><br
                /></template>
              </template>
            </td>
            <td>
              <template v-for="(flt, index) in info.flights">
                <small class="text-success">{{ flt.aport }}</small>
                {{ flt.aportName }}
                <template v-if="index < info.flights.length - 1"
                  ><br
                /></template>
              </template>
            </td>
            <td>
              <template v-for="(flt, index) in info.flights">
                {{ flt.ddate }}
                <template v-if="index < info.flights.length - 1"
                  ><br
                /></template>
              </template>
            </td>
            <td>
              <template v-for="(flt, index) in info.flights">
                {{ flt.flightNo }}
                <template v-if="index < info.flights.length - 1"
                  ><br
                /></template>
              </template>
            </td>
            <td>
              <template v-for="(flt, index) in info.flights">
                {{ flt.subclass }}
                <template v-if="index < info.flights.length - 1"
                  ><br
                /></template>
              </template>
            </td>
            <td class="text-center">
              <template v-for="(flt, index) in info.flights">
                {{ flt.dtime }}-{{ flt.atime }}
                <template v-if="index < info.flights.length - 1"
                  ><br
                /></template>
              </template>
            </td>
            <td class="text-end">{{ info.parvalue }}</td>
            <td class="text-end">{{ info.tax }}</td>
            <td class="text-end">{{ info.serviceCharge }}</td>
            <td class="text-end">{{ info.totalAmount }}</td>
            <td>
              {{ getPayTypeDesc(info.payType) }}
              <span
                class="small text-danger"
                v-if="info.payType !== 8 && info.payStatus === 0"
                >(未收)</span
              >
            </td>
            <td>
              {{ getItineraryTypeDesc(info.itineraryType) }}
              <span
                class="small text-danger"
                v-if="info.itineraryMailingStatus === 0"
                >(未寄)</span
              >
            </td>
            <td>{{ getStatusDesc(info.status) }}</td>
            <td>{{ info.itineraryPrintCount }}</td>
            <td>
              <router-link
                :to="`/flt/order/` + info.id + `/print?showPrice=1`"
                target="_blank"
                >打印</router-link
              >
            </td>
          </tr>
        </template>
      </tbody>
    </table>

    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>
  </div>
</template>

<script>
import { searchFlightOrdersToPrint } from "@/api/flight.js";
import {
  showFlightOrderStatus,
  showPayType,
  showItineraryType,
} from "@/common/common.js";
import MyDatePicker from "@/components/my-datepicker.vue";
import MyPagination from "@/components/my-pagination.vue";
import MySelectCustomer from "@/components/my-select2-customer.vue";

export default {
  components: {
    MyDatePicker,
    MyPagination,
    MySelectCustomer,
  },
  data() {
    return {
      dataList: [],

      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 25,
        pageTotal: 0,
      },

      beginDate: "",
      endDate: "",
      name: "",
      idno: "",
      status: -1,
      itineraryType: -1,
      printCount: -1,
      intlTicket: -1,
      customerId: -1,
    };
  },
  mounted: function () {
    this.search();
  },
  activated: function () {
    this.search();
  },
  methods: {
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.beginDate": this.beginDate,
        "sc.endDate": this.endDate,
        "sc.itineraryType": this.itineraryType,
        "sc.printCount": this.printCount,
        "sc.searchType": 1,
        "sc.intlTicket": this.intlTicket,
        "sc.customerId": this.customerId,
      };
      searchFlightOrdersToPrint(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
      });
    },
    quickSearch: function (status) {
      this.status = status;
      this.search();
    },
    reset: function () {
      this.beginDate = "";
      this.endDate = "";
      this.itineraryType = -1;
      this.printCount = -1;
      this.sc.pageNo = 1;
      this.search();
    },
    getStatusDesc: function (status) {
      return showFlightOrderStatus(status);
    },
    getPayTypeDesc: function (payType) {
      return showPayType(payType);
    },
    getItineraryTypeDesc: function (itineraryType) {
      return showItineraryType(itineraryType);
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>